
/**
 * Created by Administrator on 2016/11/3.
 */
var express = require('express'),
    path = require('path'),
    fs = require('fs'),
    bodyParser   = require('body-parser'),
    http         = require('http'),
    domain       = require('domain'),
    timeout      = require('connect-timeout'),
    multiparty   = require('connect-multiparty'),
    ejsMate      = require('ejs-mate'),
    session      = require('express-session'),
    cookieParser = require('cookie-parser'),
    session      = require('express-session'),
    config       = require('./config'),
    log4js       = require('./common/logger'),
    errorhandler = require('errorhandler'),
    app = express();
// 设置视图目录
app.set('views','./app/views/pages');
// 设置默认的模板引擎
app.set('view engine', 'ejs');
app.engine('ejs', ejsMate);
app.enable('trust proxy');
// app.use(consoleLogger('dev'));
app.use(timeout('10s'));
app.use(cookieParser(config.session_secret));
/*
 * 静态资源的获取以及定义静态资源的默认的存放文件夹
 * path.join() 可以传入多个参数
 * __dirname 表示当前的目录
 * */
app.use(express.static(path.join(__dirname, 'public')));

const interface = require('./interface.json');
interface.servers = config.devService;
// 初始化modelproxy接口文件
var ModelProxy = require('./modelproxy/modelproxy');
ModelProxy.init(interface);

// 定义数据解析器
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
app.use(multiparty())
// 定义cookie解析器
app.use(session({
  secret            : config.session_secret,
  name              : config.name,
  cookie            : {
      maxAge : config.cookieMaxAge
  },
  resave            : false,
  saveUninitialized : true
}));
// 引入路由
require('./routes/routes')(app);

app.use(function(req, res, next){
  
});
function errorCatch(req, res, msg){
  var deviceAgent = req.headers["user-agent"].toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);
    if(agentID){
      return res.render('mobile/app_bid/error', {
        title: '错误页面',
        errorMsg: msg ,
        dataSet:{
        },
        userInfo:{
            
        }
      });
        //
    }else{
      //  指到pc网页
      return res.render('pc/error/error', {
        title: '错误页面',
        errorMsg: msg ,
        dataSet:{
        },
        userInfo:{
            
        }
      });
    }
  }
// 监听端口
app.listen(config.port);
